package com.lingx.web.api.impl;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.lingx.model.impl.DefaultEntity;
import com.lingx.service.LingxService;
import com.lingx.service.ModelService;
import com.lingx.utils.Utils;
import com.lingx.web.api.IApi;
@Component
public class Api9025 extends AbstractDevApi implements IApi{
	@Resource
	private ModelService modelService;
	@Resource
	private JdbcTemplate jdbcTemplate;
	@Resource
	private LingxService lingxService;
	@Override
	public int getApiCode() {
		return 9025;
	}
	@Override
	public String getApiName() {
		return "克隆";
	}
	@Override
	public Map<String, Object> api(Map<String, Object> params) {
		Map<String,Object> ret=IApi.getRetMap(1, "操作成功");
		String ecode=IApi.getParamString(params, "ecode", "");
		String clone=IApi.getParamString(params, "clone", "");
		if(clone.indexOf(",")==-1) {
			ret.put("code", -1);
			ret.put("message", "模型代码与模型名称要用英文逗号隔开！");
			return ret;
		}
		String arr[]=clone.split(",");
		String newCode=arr[0];
		String newName=arr[1];
		//System.out.println(code);
		//System.out.println(newCode);
		DefaultEntity entity=modelService.getEntity(ecode);
		DefaultEntity newEntity=(DefaultEntity)modelService.clone(entity);
		newEntity.setCode(newCode);
		newEntity.setName(newName);
		modelService.save(newEntity);
		String appid=jdbcTemplate.queryForObject("select app_id from tlingx_entity where code=?",String.class,ecode);
		if(lingxService.queryForInt("select count(*) from tlingx_entity where code=?",newCode)==0)
			jdbcTemplate.update("insert into tlingx_entity(id,name,code,type,status,app_id,create_time)values(uuid(),?,?,1,1,?,?)",newName,newCode,appid,Utils.getTime());
		
		
		return ret;
	}

}
